package com.mingzhi.forum.service.impl;

import com.mingzhi.forum.common.AppResult;
import com.mingzhi.forum.common.ResultCode;
import com.mingzhi.forum.dao.BoardMapper;
import com.mingzhi.forum.exception.ApplicationException;
import com.mingzhi.forum.model.Board;
import com.mingzhi.forum.service.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //参数校验
        if (num <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //进行查询
        List<Board> result = boardMapper.selectByNum(num);
        return result;
    }

    @Override
    public List<Board> selectAllNormal() {
        //进行查询
        List<Board> result = boardMapper.selectAllNormal();
        return result;
    }

    @Override
    public Board selectById(Long id) {
        //进行校验
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    /**
     * 更新板块的帖子数量
     *
     * @param id
     */
    @Override
    public void addOneArticleCountById(Long id) {
        //进行校验
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用service层进行查询
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        int boardRow = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if (boardRow != 1) {
            //打印日志
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //进行校验
        if (id == null || id <= 0) {
            //打印日志 板块帖子数量更新失败
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //根据板块id进行板块查询
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            //打印日志 板块不存在
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id = " + id + " .");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构造板块对象
        Board updateBoard = new Board();
        updateBoard.setId(id);//板块id
        updateBoard.setArticleCount(board.getArticleCount() - 1);//在原本的帖子数量上减一
        //进行帖子数量判断 看减一之后是否为负数
        if (updateBoard.getArticleCount() < 0) {
            updateBoard.setArticleCount(0);
        }
        //将这个新的更新对象写道数据库
        int updateBoardRow = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //进行执行结果判断
        if (updateBoardRow != 1) {
            //打印日志
            log.warn(ResultCode.FAILED.toString() + ", 受影响行数不等于1 .");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
